<template>
    <div class="acupoint_list">
        <el-container>
            <el-header>
                <page-title>病例列表</page-title>
            </el-header>

            <el-main class="content">
                <div class="search_box">
                    <el-row :gutter="20" style="min-width:1024px">
                        <el-col :span="6">
                            名称：<el-input v-model="searchForm.title" style="width:50%"></el-input>
                        </el-col>
                        <el-col :span="6">
                            分类：
                            <el-select v-model="searchForm.category" placeholder="请选择" style="width:50%">
                                <el-option
                                    v-for="(item,id) in options"
                                    :key="id"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-col>
                        <el-col :span="6">
                            <el-button type="primary" @click="getData">查询结果</el-button>
                        </el-col>
                    </el-row>
                </div>

                <div class="print_box">
                    <el-row :gutter="20">
                        <el-col :span="24" style="display:flex;justify-content: flex-end">
                            <el-button @click="addOne">添加一级分类</el-button>
                            <el-button @click="addTwo">添加二级分类</el-button>
                            <el-button @click="() => {$router.push({name: 'AddAcupoint'})}">添加穴位</el-button>
                        </el-col>
                    </el-row>
                </div>

                <div class="table_box">
                    <el-table
                        :data="tableData"
                        border
                        style="width: 100%"> 
                        <el-table-column
                        align="center"
                            label="名称">
                            <template slot-scope="scope">
                                {{scope.row.title}}&nbsp;<span v-if="scope.row.category !== '1' ">({{scope.row.pointFather.title}})</span>
                            </template>
                        </el-table-column>

                        <el-table-column
                        align="center"
                            label="分类">
                            <template slot-scope="scope">
                                <span>{{scope.row.category === '3' ? '三级' : scope.row.category === '2' ? '二级' : '一级'}}</span>
                            </template>
                        </el-table-column>

                        <el-table-column
                            align="center"
                            prop="create_time"
                            label="发布时间">
                        </el-table-column>

                        <el-table-column
                        align="center"
                            label="操作"
                            width="180">
                            <template slot-scope="scope">
                                <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
                                <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <div class="pagination_box" style="text-align:right">
                        <el-pagination
                            layout="prev, pager, next"
                            :total="total"
                            :page-size = "pageSize"
                            @current-change="setCurrentPage">
                        </el-pagination>
                    </div>
                </div>

                <!-- 弹框 -->
                <el-dialog
                    :title="state === 0 ? '添加一级分类' : '添加二级分类'"
                    :visible.sync="dialogVisible"
                    width="40%"
                    :before-close="close">
                <div>
                    <el-form :model="form" :rules="rules" ref="acupointForm">
                        <el-form-item v-if="state === 1" label="选择一级分类：" label-width="120px" prop="name"  class="demo-ruleForm">
                            <el-select v-model="form.one_level" placeholder="请选择一级分类" @change="oneLevelChange">
                                <el-option v-for="(item,id) in level_one_options" :key="id" :label="item.label" :value="item.value"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item v-if="state === 1" label="已有分类：" label-width="120px" prop="name"  class="demo-ruleForm">
                            <el-input v-model="level_two_options" placeholder="暂无数据" auto-complete="off" style="width:300px" readonly=""></el-input>
                        </el-form-item>
                        <el-form-item label="分类名称：" label-width="120px" prop="title"  class="demo-ruleForm">
                            <el-input v-model="form.title" auto-complete="off" style="width:300px"></el-input>
                        </el-form-item>
                        
                        <el-form-item label="初级会员可看：" label-width="120px" prop="delivery">
                            <el-switch v-model="form.level_two"></el-switch>
                        </el-form-item>

                        <el-form-item label="游客登录可看：" label-width="120px" prop="delivery">
                            <el-switch v-model="form.level_one"></el-switch>
                        </el-form-item>

                    </el-form>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="close">取 消</el-button>
                    <el-button type="primary" @click="submit">确 定</el-button>
                </span>
                </el-dialog>
            </el-main>
        </el-container>
    </div>
</template>

<script>
import * as api from './../../../api/api'
export default {
    data() {
        return{
            //--------搜索相关---------
            searchForm: {
                title: '',
                category: ''
            },
            options: [
                {
                    value: '',
                    label: '全部'
                },
                {
                    value: '1',
                    label: '一级分类'
                },
                {
                    value: '2',
                    label: '二级分类'
                },
                {
                    value: '3',
                    label: '三级分类'
                }
            ],
            
            //-------表格相关------
            tableData: [],//表格数据
            total: 0,//数据总条数
            offset: 0,//显示的页
            pageSize: 10,//每页显示条数

            //------弹框相关-------
            dialogVisible: false,
            isEdit: 0,
            editId: '',
            state: 0,
            //一级分类
            level_one_options: [],
            //二级分类
            level_two_options: '',
            //弹框表单
            form: {
                one_level: '',
                title: '',
                delivery: true,
                level_one: false,
                level_two: false,
            },
            //表单验证规则
            rules: {
                title: [
                    { required: true, message: '请输入分类名称', trigger: 'blur' }
                ]
            },
        }
    },

    //计算属性
    computed: {

    },

    //钩子函数（页面挂载完成时触发）
    mounted() {
        this.getData()
    },

    //自定义方法
    methods: {
        getData() {
            let body = {
                model: 'Points',
                with:{
                    "0":"pointSons",
                    "1":"pointFather"
                },
                limit: this.pageSize,
                offset: this.offset,
                orderBy: "create_time desc",
                condition: {
                    is_delete: 0,
                    category:"0,!="
                }
            };
            if(this.searchForm.title){
                body.condition.title = this.searchForm.title;
            }
            if(this.searchForm.category){
                body.condition.category = this.searchForm.category;
            }
            api.getDateList(body).then(res=>{
                if(res.data.code==200){
                    this.tableData = res.data.data.data;
                    this.total = res.data.data.count * 1;
                }
            });

        },
        addOne() {
            this.isEdit = 0;
            this.state = 0;
            this.form = {
                one_level: '',
                title: '',
                delivery: true,
                level_one: false,
                level_two: false,
            };
            this.dialogVisible = true;
        },
        addTwo() {
            this.isEdit = 0;
            this.state = 1;
            this.form = {
                one_level: '',
                title: '',
                delivery: true,
                level_one: false,
                level_two: false,
            };
            let body = {
                model: 'Points',
                limit: 10000,
                offset: 0,
                orderBy: "create_time desc",
                condition: {
                    parent_id: 0
                }
            };
            api.getDateList(body).then(res => {
                if(res.data.code === 200){
                    for (const item of res.data.data.data) {
                        let one = {};
                        one.value = item.id;
                        one.label = item.title;
                        this.level_one_options.push(one);
                    }
                }
                
            });
            this.dialogVisible = true;
        },
        oneLevelChange() {
            this.form.two_level = '';
            this.level_two_options = '';
            let body = {
                model: 'Points',
                limit: 10000,
                offset: 0,
                orderBy: "create_time desc",
                condition: {
                    parent_id: this.form.one_level
                }
            };
            api.getDateList(body).then(res => {
                if(res.data.code === 200){
                    for (const item of res.data.data.data) {
                        let one = item.title;
                        this.level_two_options =this.level_two_options + one + '，';
                    }
                }
            });
        },
        close() {
            this.$refs.acupointForm.clearValidate();
            this.level_one_options = [];
            this.level_two_options = '';
            this.dialogVisible = false;
        },
        submit() {
            if(this.state === 0 && this.isEdit === 0) {
                let id = window.sessionStorage.getItem('u_id');
                let body = {
                    model:"Points",
                    data: {
                        parent_id:0,
                        publish_uid:id,
                        title: this.form.title,
                        level_one:this.form.level_one ? 1 : 0,
                        level_two:this.form.level_two ? 1 : 0,
                        category:1
                    }
                };
                api.createData(body).then(res => {
                    if(res.data.code === 200){
                        this.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        this.dialogVisible = false;
                        this.getData()
                    }
                })
            } 
            else if(this.state === 1 && this.isEdit === 0) {
                let id = window.sessionStorage.getItem('u_id');
                let body = {
                    model:"Points",
                    data: {
                        parent_id: this.form.one_level,
                        publish_uid:id,
                        title: this.form.title,
                        level_one:this.form.level_one ? 1 : 0,
                        level_two:this.form.level_two ? 1 : 0,
                        category:2
                    }
                };
                api.createData(body).then(res => {
                    if(res.data.code === 200){
                        this.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        this.dialogVisible = false;
                        this.getData()
                    }
                })
            }else 
            if(this.state === 0 && this.isEdit === 1) {
                let id = window.sessionStorage.getItem('u_id');
                let body = {
                    model:"Points",
                    data: {
                        parent_id:0,
                        publish_uid:id,
                        title: this.form.title,
                        level_one:this.form.level_one ? 1 : 0,
                        level_two:this.form.level_two ? 1 : 0,
                        category:1,
                    },
                    update_condition: {
                        id: this.editId
                    }
                };
                api.createData(body).then(res => {
                    if(res.data.code === 200){
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                        this.dialogVisible = false;
                        this.getData()
                    }
                })
            }else 
            if(this.state ===1 && this.isEdit === 1){
                let id = window.sessionStorage.getItem('u_id');
                let body = {
                    model:"Points",
                    data: {
                        parent_id: this.form.one_level,
                        publish_uid:id,
                        title: this.form.title,
                        level_one:this.form.level_one ? 1 : 0,
                        level_two:this.form.level_two ? 1 : 0,
                        category:2,
                    },
                    update_condition: {
                        id: this.editId
                    }
                };
                api.createData(body).then(res => {
                    if(res.data.code === 200){
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                        this.dialogVisible = false;
                        this.getData()
                    }
                })
            }
        },

        //编辑
        edit(row) {
            this.isEdit = 1;
            this.editId = row.id;
            this.form = {
                one_level: '',
                title: '',
                delivery: true,
                level_one: false,
                level_two: false,
            };
            if(row.category === '1') {
                this.state = 0;
                this.dialogVisible = true;
                this.form.title = row.title;
                this.form.level_one = row.level_one === '0' ? false : true;
                this.form.level_two = row.level_two === '0' ? false : true;
            }else
            if(row.category === '2') {
                this.state = 1;
                let body = {
                    model: 'Points',
                    limit: 10000,
                    offset: 0,
                    orderBy: "create_time desc",
                    condition: {
                        parent_id: 0
                    }
                };
                api.getDateList(body).then(res => {
                    if(res.data.code === 200){
                        for (const item of res.data.data.data) {
                            let one = {};
                            one.value = item.id;
                            one.label = item.title;
                            this.level_one_options.push(one);
                        }
                    }
                });
                this.form.title = row.title;
                this.form.one_level = row.parent_id;
                this.oneLevelChange();
                this.form.level_one = row.level_one === '0' ? false : true;
                this.form.level_two = row.level_two === '0' ? false : true;
                this.dialogVisible = true;
            }else
            if(row.category === '3') {
                this.$router.push({name:'EditAcupoint',params:{data:row}})
            }
        },

        //删除
        del(row) {
            let id = window.sessionStorage.getItem('u_id');
            let body = {
                model:"Points",
                data: {
                    is_delete: '1'
                },
                update_condition: {
                    id: row.id,
                    category:row.category,
                }
            };
            api.createData(body).then(res => {
                if(res.data.code === 200){
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                    this.dialogVisible = false;
                    this.getData()
                }else if(res.data.code === 201){
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    });
                }
            })
        },

        //分页
        setCurrentPage(currentPage) {
            this.offset = currentPage - 1;
            this.getData();
        },
    },
}
</script>

<style lang="scss" scoped>
    .acupoint_list{
        .el-header{
            padding: 0;
        }
        .el-date-editor .el-range-separator{
            width: 20px;
        }
        .el-table__header tr th{
            background-color: #f6f6f6;
        }
        .content{
            padding: 20px 80px;
            overflow: hidden;
            .table_box{
                margin-top: 40px;
            }
            .pagination_box{
                padding: 4px;
                background-color: #f6f6f6;
                border: 1px solid #eee;
                border-top: none;
            }
        }
    }
</style>
